package at.ltasat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import at.ltasat.classifier.io.ClazzManagerIO;
import at.ltasat.util.IO;

public final class ArgumentClazzDataClazzManagerIO extends ClazzManagerIO<ArgumentClazzData> {
	@Override
	public ArgumentClazzData[] createArray(int length) {
		return new ArgumentClazzData[length];
	}

	@Override
	public byte[] toRaw(ArgumentClazzData data) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			IO.writeString(out, data.getArgument());
			IO.writeInteger(out, data.nProcessors());
			IO.writeBoolean(out, data.canCreateMore());
		} catch (IOException e) {
			assert false;
		}
		return out.toByteArray();
	}

	@Override
	public ArgumentClazzData toObject(byte[] raw) {
		ByteArrayInputStream in = new ByteArrayInputStream(raw);
		String argument = null;
		int nProcessors = -1;
		boolean canCreateMore = false;
		try {
			argument = IO.readString(in);
			nProcessors = IO.readInteger(in);
			canCreateMore = IO.readBoolean(in);
		} catch (IOException e) {
			assert false;
		}
		return new ArgumentClazzData(argument, nProcessors, canCreateMore);
	}
}
